package tasks.task07;

import info.clearthought.layout.TableLayout;

import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class task07 {
    private static File MIDI = null;     // Midi File
    private static Sequencer SEQ = null; // Sequencer
    private static double BOR = 10;      // Border
    private static String TTLE;          // Title name
    private static int FH = 174;         // Frame height

    public static void main(final String[] args) {

        // Create a frame
        final JFrame frame = new JFrame("MIDI Media Player");
        frame.setBounds(0, 0, 650, FH);

        double size[][] = {{BOR, 200, BOR, 200, BOR, 200, BOR}, // Columns
                {BOR, 114, BOR, TableLayout.FILL, BOR}}; // Rows

        frame.setLayout(new TableLayout(size));
        frame.setResizable(false);

        // Creating Icons
        ImageIcon openIcon = new ImageIcon("imgs/open.jpg");
        ImageIcon playIcon = new ImageIcon("imgs/play.jpg");
        ImageIcon stopIcon = new ImageIcon("imgs/stop.jpg");

        // Rescaling Images and creating buttons
        Image img = openIcon.getImage();
        Image newImg = img.getScaledInstance(200, 114, java.awt.Image.SCALE_SMOOTH);
        ImageIcon resizedIcon = new ImageIcon(newImg);
        JButton openButton = new JButton(resizedIcon);

        img = playIcon.getImage();
        newImg = img.getScaledInstance(200, 114, java.awt.Image.SCALE_SMOOTH);
        resizedIcon = new ImageIcon(newImg);
        JButton playButton = new JButton(resizedIcon);

        img = stopIcon.getImage();
        newImg = img.getScaledInstance(200, 114, java.awt.Image.SCALE_SMOOTH);
        resizedIcon = new ImageIcon(newImg);
        JButton stopButton = new JButton(resizedIcon);

        final JToggleButton Mute1 = new JToggleButton("Mute 1 track");
        final JToggleButton Mute2 = new JToggleButton("Mute 2 track");
        final JToggleButton Mute3 = new JToggleButton("Mute 3 track");

        Mute1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!SEQ.getTrackMute(0)) {
                    SEQ.setTrackMute(0, true);
                    Mute1.setSelected(true);
                } else {
                    SEQ.setTrackMute(0, false);
                    Mute1.setSelected(false);
                }
            }
        });


        Mute2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!SEQ.getTrackMute(1)) {
                    SEQ.setTrackMute(1, true);
                    Mute2.setSelected(true);
                } else {
                    SEQ.setTrackMute(1, false);
                    Mute2.setSelected(false);
                }
            }
        });


        Mute3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!SEQ.getTrackMute(2)) {
                    SEQ.setTrackMute(2, true);
                    Mute3.setSelected(true);
                } else {
                    SEQ.setTrackMute(2, false);
                    Mute3.setSelected(false);
                }
            }
        });

         // OPEN
        openButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (MIDI != null) {
                    SEQ.stop();
                }

                FileNameExtensionFilter filter = new FileNameExtensionFilter("MIDI (*.mid)", "mid");
                JFileChooser chooser = new JFileChooser("../Java/src/tasks/task07/");
                chooser.addChoosableFileFilter(filter);
                int option = chooser.showOpenDialog(chooser);

                if (option == JFileChooser.APPROVE_OPTION) {
                    TTLE = chooser.getSelectedFile().getName();
                    MIDI = chooser.getSelectedFile();
                    Sequence sequence;
                    try {
                        sequence = MidiSystem.getSequence(MIDI);
                        SEQ = MidiSystem.getSequencer();
                        SEQ.open();
                        SEQ.setSequence(sequence);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        );

        // PLAY
        playButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (MIDI != null) {
                    SEQ.start();
                    frame.setTitle("Now playing: " + TTLE);
                }
            }
        }
        );

        // STOP
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (MIDI != null) {
                    SEQ.stop();
                }
            }
        }
        );

        // Adding elements
        frame.add(openButton, "1,1");
        frame.add(playButton, "3,1");
        frame.add(stopButton, "5,1");

        frame.add(Mute1, "1,2");
        frame.add(Mute2, "3,2");
        frame.add(Mute3, "5,2");

        frame.getContentPane().setBackground(Color.LIGHT_GRAY); // Setting white background
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close on [X]
        frame.setLocationRelativeTo(null); // Center to Display
        frame.setVisible(true);
    }
}
